//
//  ScheduleFilterViewController.m
//  FMS
//
//  Created by Steven on 10/12/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "ScheduleFilterViewController.h"
#import "ScheduleInspectionViewController.h"
@interface ScheduleFilterViewController ()

@end

@implementation ScheduleFilterViewController
@synthesize delegate;
@synthesize dictFilter;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        dictLanguage = [ApplicationDelegate.arrayLanguage objectAtIndex:0];
        
    }
    return self;
}
#pragma mark util methods
- (void)selectServiceType:(id)sender{
    UIButton *btnSender = (UIButton *)sender;
    if (btnBuildingType == btnSender)
    {
        indexServiceType = Building_Type;
        iconRadioBuilding.image = [UIImage imageNamed:@"iconRadio.png"];
        iconRadioEquipment.image = [UIImage imageNamed:@"iconRadioUnSelect.png"];
    }
    else
    {
        indexServiceType = Facility_Type;
        iconRadioBuilding.image = [UIImage imageNamed:@"iconRadioUnSelect.png"];
        iconRadioEquipment.image = [UIImage imageNamed:@"iconRadio.png"];
    }
    if (dictFilter == nil)
        dictFilter = [[NSMutableDictionary alloc] init];
    if ([dictFilter respondsToSelector:@selector(setObject:forKey:)])
        ;
    else
        dictFilter = [dictFilter mutableCopy];
    [dictFilter setObject:[NSNumber numberWithInt:indexServiceType] forKey:@"itemType"];
}

- (IBAction) selectFilter:(UIButton *)sender {
    int filterType = sender.tag;
    //init controller to filter value
    FilterViewController *controller = [[FilterViewController alloc] initWithStyle:UITableViewStylePlain filterType:filterType];
    controller.delegate = self;
    popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
    [popoverController presentPopoverFromRect:sender.frame inView:[sender superview]permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (void)selectCategory:(UIButton *)btnSender{
    FilterViewController *controller = [[FilterViewController alloc] initWithStyle:UITableViewStylePlain filterType:filter_category];
    controller.delegate = self;
    popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
    [popoverController presentPopoverFromRect:btnSender.frame inView:btnSender.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (void)loadScheduleInfo{
    if (dictFilter) {
        typeCategory = [[dictFilter objectForKey:@"categoryId"] intValue];
        
        [btnCategory setTitle:[dictFilter objectForKey:@"scheduleCategory"] forState:UIControlStateNormal];
        [btnService setTitle:[dictFilter objectForKey:@"service"] forState:UIControlStateNormal];
        [btnCamp setTitle:[dictFilter objectForKey:@"camp"] forState:UIControlStateNormal];
        [btnUnit setTitle:[dictFilter objectForKey:@"unit"] forState:UIControlStateNormal];
        [btnBuilding setTitle:[dictFilter objectForKey:@"building"] forState:UIControlStateNormal];
        [btnFloor setTitle:[dictFilter objectForKey:@"floor"] forState:UIControlStateNormal];
        [btnRoom setTitle:[dictFilter objectForKey:@"room"] forState:UIControlStateNormal];
        indexServiceType = [[dictFilter objectForKey:@"itemType"] intValue];
        
        [self showItemType];
    }
    else{
        [btnCategory setTitle:[dictLanguage objectForKey:@"schedule.title-select-category-program"] forState:UIControlStateNormal];
        [btnService setTitle:[dictLanguage objectForKey:@"filter.select-service"] forState:UIControlStateNormal];
        [btnCamp setTitle:[dictLanguage objectForKey:@"filter.select-camp"] forState:UIControlStateNormal];
        [btnUnit setTitle:[dictLanguage objectForKey:@"filter.select-unit"] forState:UIControlStateNormal];
        [btnBuilding setTitle:[dictLanguage objectForKey:@"filter.select-building"] forState:UIControlStateNormal];
        [btnFloor setTitle:[dictLanguage objectForKey:@"filter.select-floor"] forState:UIControlStateNormal];
        [btnRoom setTitle:[dictLanguage objectForKey:@"filter.select-room"] forState:UIControlStateNormal];
    }
}
- (void)showItemType{
    if (typeCategory == Inspection) {
        btnEquipmentType.hidden = YES;
        [self selectServiceType:btnBuilding];
    }
    else
    {
        btnEquipmentType.hidden = NO;
        if (indexServiceType == Building_Type)
        {
            [self selectServiceType:btnBuilding];
        }
        else
        {
            [self selectServiceType:btnEquipmentType];
        }
        
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self addTopBar];
    if (self.navBar)
    {
        self.navBar.navBarViewDelegate = self;
        self.navBar.titleLabel.text = [dictLanguage objectForKey:@"schedule.title-schedule"];
    }
    
    btnCategory = [[CustomButton alloc] initWithFrame:CGRectMake(495, 10, 350, 30)];
    [btnCategory addTarget:self action:@selector(selectCategory:) forControlEvents:UIControlEventTouchUpInside];
    
    btnService = [[CustomButton alloc] initWithFrame:CGRectMake(495, 10, 350, 30)];
    btnUnit = [[CustomButton alloc] initWithFrame:CGRectMake(495, 10, 350, 30)];
    btnCamp = [[CustomButton alloc] initWithFrame:CGRectMake(495, 10, 350, 30)];
    btnBuilding = [[CustomButton alloc] initWithFrame:CGRectMake(495, 10, 350, 30)];
    btnFloor = [[CustomButton alloc] initWithFrame:CGRectMake(495, 10, 350, 30)];
    btnRoom = [[CustomButton alloc] initWithFrame:CGRectMake(495, 10, 350, 30)];
    [btnCategory converCombobox];
    [btnService converCombobox];
    [btnUnit converCombobox];
    [btnCamp converCombobox];
    [btnBuilding converCombobox];
    [btnFloor converCombobox];
    [btnRoom converCombobox];
    
    btnService.tag = filter_service;
    btnCamp.tag = filter_camp;
    btnUnit.tag = filter_unit;
    btnBuilding.tag = filter_building;
    btnFloor.tag = filter_floor;
    btnRoom.tag = filter_room;
    
    [btnService addTarget:self action:@selector(selectFilter:) forControlEvents:UIControlEventTouchUpInside];
    [btnCamp addTarget:self action:@selector(selectFilter:) forControlEvents:UIControlEventTouchUpInside];
    [btnUnit addTarget:self action:@selector(selectFilter:) forControlEvents:UIControlEventTouchUpInside];
    [btnBuilding addTarget:self action:@selector(selectFilter:) forControlEvents:UIControlEventTouchUpInside];
    [btnFloor addTarget:self action:@selector(selectFilter:) forControlEvents:UIControlEventTouchUpInside];
    [btnRoom addTarget:self action:@selector(selectFilter:) forControlEvents:UIControlEventTouchUpInside];
    
    iconRadioBuilding = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 19, 19)];
    iconRadioBuilding.image = [UIImage imageNamed:@"iconRadio.png"];
    
    iconRadioEquipment = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 19, 19)];
    iconRadioEquipment.image = [UIImage imageNamed:@"iconRadioUnSelect.png"];
    
    
    btnBuildingType = [[UIButton alloc] initWithFrame:CGRectMake(495, 10, 120, 30)];
    [btnBuildingType setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btnBuildingType setContentEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    btnEquipmentType = [[UIButton alloc] initWithFrame:CGRectMake(630, 10, 120, 30)];
    [btnEquipmentType setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btnEquipmentType setContentEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    
    [btnBuildingType setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btnEquipmentType setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btnBuildingType addSubview:iconRadioBuilding];
    
    iconRadioBuilding.center = CGPointMake(10, btnBuildingType.center.y/2 + 2);
    
    [btnEquipmentType addSubview:iconRadioEquipment];
    
    iconRadioEquipment.center = CGPointMake(10, btnEquipmentType.center.y/2 + 2);
    
    
    [btnBuildingType setTitle:[dictLanguage objectForKey:@"schedule.title-building"] forState:UIControlStateNormal];
    [btnEquipmentType setTitle:[dictLanguage objectForKey:@"schedule.title-equipment"] forState:UIControlStateNormal];
    
    [btnBuildingType addTarget:self action:@selector(selectServiceType:) forControlEvents:UIControlEventTouchUpInside];
    [btnEquipmentType addTarget:self action:@selector(selectServiceType:) forControlEvents:UIControlEventTouchUpInside];
    
    [self loadScheduleInfo];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark FilterView Delegate
- (void) didSelectFilter:(NSMutableDictionary *)filterDict {
    [self didmissPopoverController];
    if (dictFilter == nil)
        dictFilter = [[NSMutableDictionary alloc] init];
    if (![dictFilter respondsToSelector:@selector(setObject:forKey:)]) {
        dictFilter = [dictFilter mutableCopy];
    }
    switch ([[filterDict objectForKey:@"filterType"] intValue]) {
        case filter_service:
        {
            [btnService setTitle:[filterDict objectForKey:@"filterName"] forState:UIControlStateNormal];
            [dictFilter setObject:[filterDict objectForKey:@"filterName"] forKey:@"service"];
        }
            break;
        case filter_camp:
        {
            [btnCamp setTitle:[filterDict objectForKey:@"filterName"] forState:UIControlStateNormal];
            [dictFilter setObject:[filterDict objectForKey:@"filterName"] forKey:@"camp"];
        }
            break;
        case filter_unit:
        {
            [btnUnit setTitle:[filterDict objectForKey:@"filterName"] forState:UIControlStateNormal];
            [dictFilter setObject:[filterDict objectForKey:@"filterName"] forKey:@"unit"];
        }
            break;
        case filter_building:
        {
            [btnBuilding setTitle:[filterDict objectForKey:@"filterName"] forState:UIControlStateNormal];
            [dictFilter setObject:[filterDict objectForKey:@"filterName"] forKey:@"building"];
        }
            break;
        case filter_floor:
        {
            [btnFloor setTitle:[filterDict objectForKey:@"filterName"] forState:UIControlStateNormal];
            [dictFilter setObject:[filterDict objectForKey:@"filterName"] forKey:@"floor"];
        }
            break;
        case filter_room:
        {
            [btnRoom setTitle:[filterDict objectForKey:@"filterName"] forState:UIControlStateNormal];
            [dictFilter setObject:[filterDict objectForKey:@"filterName"] forKey:@"room"];
        }
            break;
        case filter_category:
        {
            [btnCategory setTitle:[filterDict objectForKey:@"filterName"] forState:UIControlStateNormal];
            [dictFilter setObject:[NSString stringWithFormat:@"%@",[filterDict objectForKey:@"filterName"]] forKey:@"scheduleCategory"];
            [dictFilter setObject:[filterDict objectForKey:@"index"] forKey:@"categoryId"];
            if (typeCategory != [[filterDict objectForKey:@"index"] intValue]) {
                [dictFilter setObject:[NSNumber numberWithInt:0] forKey:@"categoryTypeId"];
                [dictFilter setObject:@"" forKey:@"categoryType"];
                if ([[filterDict objectForKey:@"index"] intValue] == Inspection) {
                    indexServiceType = Building_Type;
                }
            }
            typeCategory = [[filterDict objectForKey:@"index"] intValue];
            [self showItemType];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - NavViewDelegate

- (void)didClickedLeftButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didClickedRightButton
{
    
    
    NSString *messageError = nil;
    if ([[btnCategory titleForState:UIControlStateNormal] isEqualToString:[dictLanguage objectForKey:@"schedule.title-select-category-program"]]) {
        messageError = [dictLanguage objectForKey:@"schedule.title-required-select-category-program"];
    }
    else if ([[btnService titleForState:UIControlStateNormal] isEqualToString:[dictLanguage objectForKey:@"filter.select-service"]]) {
        messageError = [dictLanguage objectForKey:@"filter.required-select-service"];
    }
    else if ([[btnCamp titleForState:UIControlStateNormal] isEqualToString:[dictLanguage objectForKey:@"filter.select-camp"]]) {
        messageError = [dictLanguage objectForKey:@"filter.required-select-camp"];
    }
    else if ([[btnUnit titleForState:UIControlStateNormal] isEqualToString:[dictLanguage objectForKey:@"filter.select-unit"]]) {
        messageError = [dictLanguage objectForKey:@"filter.required-select-unit"];
    }
    else if ([[btnBuilding titleForState:UIControlStateNormal] isEqualToString:[dictLanguage objectForKey:@"filter.select-building"]]) {
        messageError = [dictLanguage objectForKey:@"filter.required-select-building"];
    }
    else if ([[btnFloor titleForState:UIControlStateNormal] isEqualToString:[dictLanguage objectForKey:@"filter.select-floor"]]) {
        messageError = [dictLanguage objectForKey:@"filter.required-select-floor"];
    }
    else if ([[btnRoom titleForState:UIControlStateNormal] isEqualToString:[dictLanguage objectForKey:@"filter.select-room"]]) {
        messageError = [dictLanguage objectForKey:@"filter.required-select-room"];
    }
    if (messageError != nil) {
        [self showErrorMessage:messageError];
        return;
    }
    
    if (dictFilter == nil) {
        dictFilter = [NSMutableDictionary dictionaryWithObjectsAndKeys:[btnCategory titleForState:UIControlStateNormal],@"scheduleCategory",[btnCamp titleForState:UIControlStateNormal],@"camp",[btnService titleForState:UIControlStateNormal],@"service",[btnUnit titleForState:UIControlStateNormal],@"unit",[btnBuilding titleForState:UIControlStateNormal],@"building",[btnFloor titleForState:UIControlStateNormal],@"floor",[btnRoom titleForState:UIControlStateNormal],@"room",[NSNumber numberWithInt:0],@"categoryTypeId",nil];
        
    }
    [dictFilter setObject:[NSNumber numberWithInt:typeCategory] forKey:@"categoryId"];
    [dictFilter setObject:[NSNumber numberWithInt:indexServiceType] forKey:@"itemType"];
    
    if ([delegate isKindOfClass:[ScheduleInspectionViewController class]]) {
        [((ScheduleInspectionViewController*)delegate).dictSchedule addEntriesFromDictionary:dictFilter];
        [((ScheduleInspectionViewController*)delegate) reloadInfoSchedule];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        
        ScheduleInspectionViewController *controller = [[ScheduleInspectionViewController alloc] initWithNibName:@"ScheduleInspectionViewController" bundle:[NSBundle mainBundle] schedule:dictFilter];
        controller.delegate = self;
        [self.navigationController pushViewController:controller animated:YES];
    }
    
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(172, 14, 300, 22)];
    lblTitle.backgroundColor = [UIColor clearColor];
    lblTitle.textColor = [UIColor darkGrayColor];
    lblTitle.font = [UIFont fontWithName:FMS_REGULAR_FONT size:15];
    switch (indexPath.row) {
        case 0:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-category-program"];

            [cell.contentView addSubview:btnCategory];
        }
            break;
        case 1:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-item-type"];

            [cell.contentView addSubview:btnBuildingType];
            [cell.contentView addSubview:btnEquipmentType];
        }
            break;
        case 2:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-service"];

            [cell.contentView addSubview:btnService];
        }
            break;
        case 3:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-camp"];
            
            [cell.contentView addSubview:btnCamp];
            
        }
            break;
        case 4:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-unit"];
            
            [cell.contentView addSubview:btnUnit];
        }
            break;
        case 5:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-building"];
            
            [cell.contentView addSubview:btnBuilding];
        }
            break;
        case 6:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-floor"];
            
            [cell.contentView addSubview:btnFloor];
        }
            break;
        case 7:
        {
            lblTitle.text = [dictLanguage objectForKey:@"schedule.title-room"];
            
            [cell.contentView addSubview:btnRoom];
        }
            break;
            
        default:
            break;
    }
    [cell.contentView addSubview:lblTitle];
    // Configure the cell...

    return cell;
}


@end
